package calendar;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import java.util.Date;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/* this class provides the panel for creating events */
class CreateEventPanel extends CalendarPanel {

  private static final long serialVersionUID = 343948393L;

  JLabel panelTitle, eventType;
  JRadioButton privateButton, publicButton, groupButton;
  ButtonGroup buttonGroup;
  TextInputPanel eventDescription;
  TimePanel startTimePanel, endTimePanel;
  JButton createEventButton;
  JButton editEventButton;
  JPanel buttonPanel;
  int type;

  CreateEventPanel()
  {
    panelTitle = new JLabel( " Create Event" );
    eventType = new JLabel( " Event Type:" );
    privateButton = new JRadioButton( "Private Event" );
    publicButton = new JRadioButton( "Public Event" );
    groupButton = new JRadioButton( "Group Event" );
    eventDescription = new TextInputPanel( " Description", 64 );
    startTimePanel = new TimePanel( " Start: " );
    endTimePanel = new TimePanel( " End:   " );
    createEventButton = new JButton( "Create New Event" );
    editEventButton = new JButton( "Edit Event" );
    buttonPanel = new JPanel();

    eventType.setAlignmentY( Component.LEFT_ALIGNMENT );
    eventType.setAlignmentX( Component.TOP_ALIGNMENT );
    this.setAlignmentY( Component.LEFT_ALIGNMENT );
    this.setAlignmentX( Component.TOP_ALIGNMENT );
  
    Dimension eventDescriptionSize = new Dimension( 300, 20 );
    eventDescription.setMaximumSize( eventDescriptionSize );
    eventDescription.setMinimumSize( eventDescriptionSize );
    eventDescription.setPreferredSize( eventDescriptionSize );

    Dimension createEventButtonSize = new Dimension( 150, 20 );
    createEventButton.setMaximumSize( createEventButtonSize );
    createEventButton.setMinimumSize( createEventButtonSize );
    createEventButton.setPreferredSize( createEventButtonSize );

    Dimension editEventButtonSize = new Dimension( 100, 20 );
    editEventButton.setMaximumSize( editEventButtonSize );
    editEventButton.setMinimumSize( editEventButtonSize );
    editEventButton.setPreferredSize( editEventButtonSize );

    buttonGroup = new ButtonGroup();
    buttonGroup.add( privateButton );
    buttonGroup.add( publicButton );
    buttonGroup.add( groupButton );

    buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.X_AXIS ) );
    buttonPanel.add( Box.createHorizontalStrut( 20 ) );
    buttonPanel.add( createEventButton );
    buttonPanel.add( Box.createHorizontalStrut( 20 ) );
    buttonPanel.add( editEventButton );

    privateButton.addActionListener( this );
    publicButton.addActionListener( this );
    groupButton.addActionListener( this );
    createEventButton.addActionListener( this );
    editEventButton.addActionListener( this );

    editEventButton.setEnabled( false );

    setLayout( new GridBagLayout() );
    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;

    c.gridx = 0; c.gridy = 0; c.ipadx = 2;
    add( panelTitle, c );
    c.gridx = 0; c.gridy = 1; c.ipadx = 0;
    add( eventType, c );
    c.gridx = 0; c.gridy = 2; c.ipadx = 0;
    add( privateButton, c );
    c.gridx = 0; c.gridy = 3; c.ipadx = 0;
    add( publicButton, c );
    c.gridx = 0; c.gridy = 4; c.ipadx = 0;
    add( groupButton, c );
    c.gridx = 0; c.gridy = 5; c.ipadx = 2;
    add( eventDescription, c );
    c.gridx = 0; c.gridy = 6; c.ipadx = 2;
    add( startTimePanel, c );
    c.gridx = 0; c.gridy = 7; c.ipadx = 2;
    add( endTimePanel, c );
    c.gridx = 0; c.gridy = 8; c.ipady = 4;
    add( buttonPanel, c );

    reset();
  }

  /* load an event into this panel with all event details */
  public void loadEvent( Event event )
  {
    if ( event.getType() == 1 ) privateButton.setSelected( true );
    else if ( event.getType() == 2 ) publicButton.setSelected( true );
    else if ( event.getType() == 3 ) groupButton.setSelected( true );
    this.type = event.getType();

    privateButton.setEnabled( false );
    publicButton.setEnabled( false );
    groupButton.setEnabled( false );
    editEventButton.setEnabled( true );

    eventDescription.setText( event.getDesc() );

    startTimePanel.setTime( event.getStart() );
    endTimePanel.setTime( event.getEnd() );
  }

  public void setButtonsEnabled()
  {
    privateButton.setEnabled( true );
    publicButton.setEnabled( true );
    groupButton.setEnabled( true );
  }

  public void setEditButtonDisabled()
  {
    editEventButton.setEnabled( false );
  }

  public boolean areButtonsDisabled()
  {
    return ( !privateButton.isEnabled() &&
         !publicButton.isEnabled() &&
         !groupButton.isEnabled() );
  }

  /* reset this panel (clear it) */
  public void reset()
  {
    privateButton.setSelected( true );
    this.type = 1;
    eventDescription.setText( "" );

    Calendar cal = Calendar.getInstance();
    Date now = cal.getTime();
    startTimePanel.setTime( now );
    endTimePanel.setTime( now );
  }

  public int getType()
  {
    return this.type;
  }

  public Date getStartDate()
  {
    return this.startTimePanel.getDate();
  }

  public Date getEndDate()
  {
    return this.endTimePanel.getDate();
  }

  public String getDesc()
  {
    return this.eventDescription.getText();
  }

  public void actionPerformed(ActionEvent e) {
    
    int eventId = -1;
    if ( e.getSource() == privateButton )
    {
      this.type = 1;
    }
    else if ( e.getSource() == publicButton )
    {
      this.type = 2;
    }
    else if ( e.getSource() == groupButton )
    {
      this.type = 3;
    }
    else if ( e.getSource() == createEventButton )
    {
      eventId = CalEvent.CREATE_EVENT;
      CalEvent calEvent = new CalEvent( this, eventId );
      fireCalEvent( calEvent );
    }
    else if ( e.getSource() == editEventButton )
    {
      eventId = CalEvent.EDIT_EVENT;
      CalEvent calEvent = new CalEvent( this, eventId );
      fireCalEvent( calEvent );
    }
    else return;
  }
}
